{extend name="public/base" /}

{block name="content"}
<!--页面路径 和 页面标题-->
<div class="page-title">

    <div class="title-env">
        <h1 class="title">轮播图管理</h1>
        <p class="description">所有轮播图</p>
    </div>

    <div class="breadcrumb-env">

        <ol class="breadcrumb bc-1">
            <li>
                <a href="/admin"><i class="fa-home"></i>Home</a>
            </li>
            <li>
                <a href="#">系统设置</a>
            </li>
            <li class="active">
                <strong>轮播图设置</strong>
            </li>
        </ol>

    </div>

</div>

<div class="row">
    <div class="col-md-12">

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">轮播图列表</font></font></h3>
                <div class="panel-options">
                    <button class="btn btn-icon btn-success" data-toggle="modal" data-target="#add-banner">
                        <i class="fa-plus"></i> 添加轮播图
                    </button>
                </div>
            </div>

            <div class="panel-body panel-border">
                <div class="row">
                    <div class="col-sm-12">
                        <table class="table">
                            <thead>
                            <tr>
                                <th style="width: 8%">排序</th>
                                <th>图片</th>
                                <th>链接</th>
                                <th style="width: 10%">位置</th>
                                <th>状态</th>
                                <th style="width: 15%">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {foreach $lists as $key => $list}
                            <tr>
                                <td>{$list.order}</td>
                                <td>
                                    <img src="{$list.image}" width="180px" height="120px">
                                </td>
                                <td>{$list.url}</td>
                                <td>{$list.position}</td>
                                <td>
                                    <input type="checkbox" onclick="change_status({$list.id},'banner','status')" {if condition="$list.status == 1"}checked=""{/if}  class="iswitch iswitch-success">
                                </td>
                                <td>
                                    <button class="btn btn-icon btn-success" data-toggle="modal" data-target="#edit-banner" data-id="{$list.id}">
                                        <i class="fa-edit"></i>
                                    </button>

                                    <button class="btn btn-icon btn-red" onclick="deleted({$list.id},'banner')">
                                        <i class="fa-trash"></i>
                                    </button>
                                </td>
                            </tr>
                            {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <label class="col-md-6 control-label">共 {$total} 条记录</label>
                    <div class="col-md-6 text-right">
                        <nav aria-label="Page navigation">
                            {$page}
                        </nav>
                    </div>
                </div>
            </div>

        </div>

    </div>
</div>
{/block}

{block name="modal"}

<!--添加分类模态框-->
<div class="modal fade custom-width" id="add-banner" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" style="width: 50%">
        <form id="add-form" method="post" class="form-horizontal" action="/admin/system/addBannerPost" id="add-form">
            <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">添加轮播图</h4>
                </div>

                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label"></label>
                        <div class="col-sm-7 text-center">
                            <div id="advancedDropzone" class="droppable-area" style="border-radius: 0%;width: 180px;height: 120px;line-height: 120px;">
                                点击上传
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">排序</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" name="order" value="0">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">链接</label>
                            <div class="col-sm-9">
                                <script type="text/javascript">
                                    jQuery(document).ready(function($)
                                    {
                                        $("#s2example-1").select2({
                                            placeholder: '选择链接视频...',
                                            allowClear: true
                                        }).on('select2-open', function()
                                        {
                                            // Adding Custom Scrollbar
                                            $(this).data('select2').results.addClass('overflow-hidden').perfectScrollbar();
                                        });

                                    });
                                </script>

                                <select class="form-control" id="s2example-1" name="url">
                                    <option></option>
                                    {foreach $videos as $video}
                                    <option value="{$video.id}">{$video.name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">位置</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="position">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-6">
                                <label class="radio-inline"><input type="radio" name="status" checked="1" value="1">发布</label>
                                <label class="radio-inline"><input type="radio" name="status" value="2">隐藏</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <input type="hidden" value="" name="file_url" id="file_url">
                    <input type="button" class="btn btn-info btn-single pull-right" id="add-button" value="Save changes">
                </div>
            </div>
        </form>
    </div>
</div>


<!--编辑分类模态框-->

<div class="modal fade" id="edit-banner" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <form id="edit-form" method="post" class="form-horizontal" action="/admin/system/editBannerPost" id="edit-form">
            <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">编辑分类</h4>
                </div>

                <div class="modal-body">
                    <input type="hidden" name="id" id="banner_id" value="">
                    <div class="row">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">原图</label>
                            <div class="col-sm-4">
                                <div class="droppable-area" style="border-radius: 0%;width:180px;height: 120px;">
                                    <img src="" id="show-image" style="width: 100%;">
                                </div>
                            </div>
                            <div class="col-sm-6 text-center">
                                <div id="edit-advancedDropzone" class="droppable-area" style="border-radius: 0%;width: 180px;height: 120px;line-height: 120px;">
                                    点击上传
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">排序</label>
                            <div class="col-sm-2">
                                <input type="text" class="form-control" name="order" id="order" value="0">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">链接</label>
                            <div class="col-sm-9">
                                <script type="text/javascript">
                                    jQuery(document).ready(function($)
                                    {
                                        $("#s2example-2").select2({
                                            placeholder: '选择链接视频...',
                                            allowClear: true
                                        }).on('select2-open', function()
                                        {
                                            // Adding Custom Scrollbar
                                            $(this).data('select2').results.addClass('overflow-hidden').perfectScrollbar();
                                        });

                                    });
                                </script>

                                <select class="form-control" id="s2example-2" name="url">
                                    <option></option>
                                    {foreach $videos as $video}
                                    <option value="{$video.id}"}>{$video.name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">位置</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="position" id="position">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态</label>
                            <div class="col-sm-6">
                                <label class="radio-inline"><input type="radio" name="status" checked="1" value="1">发布</label>
                                <label class="radio-inline"><input type="radio" name="status" value="2">隐藏</label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <input type="hidden" value="" name="file_url" id="edit-file_url">
                    <input type="button" class="btn btn-info" id="edit-button" value="Save changes">
                </div>
            </div>
        </form>
    </div>
</div>

<div class="modal fade bs-example-modal-lg" id="upload-show" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">上传进度</h4>
            </div>
            <div class="modal-body">
                <div class="page-container">
                    <div class="main-content ">
                        <div class="row table-responsive">
                            <table class="table table-bordered table-striped" id="example-dropzone-filetable">
                                <thead>
                                <tr>
                                    <th class="text-center">#</th>
                                    <th>Name</th>
                                    <th>Upload Progress</th>
                                    <th>Size</th>
                                    <th>Status</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    <td colspan="5">Files list will appear here</td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>



{/block}

{block name="js"}
<script src="/static/assets/js/dropzone/dropzone.js"></script>
<script>
    $('#edit-banner').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var modal = $(this);
        $.ajax({
            url:'/admin/system/edit',
            method: 'POST',
            dataType: 'json',
            data:{
                id:id,
            },
            success: function(res)
            {
                if(res.code == 0) {
                    modal.find('.modal-body #banner_id').val(res.data.id);
                    modal.find('.modal-body #order').val(res.data.order);

                    modal.find('.modal-body #position').val(res.data.position);
                    modal.find('.modal-body #show-image').attr("src",res.data.image);
                    $("#edit-banner #edit-file_url").val(res.data.image);
                    $("#edit-banner :radio[name='status'][value='" + res.data.status + "']").prop("checked", "checked");
                    $("#edit-banner :select[name='url'][value='" + res.data.url + "']").prop("selected", "selected");

                }else{
                    error(res.message);
                }
            }
        });
    });

    //添加分类表单提交
    $("#add-button").click(function(){
        var targetUrl = $("#add-form").attr("action");
        var data = $("#add-form").serialize();
        $.ajax({
            type:'post',
            url:targetUrl,
            cache: false,
            data:data,
            dataType:'json',
            success:function(data){
                if(data.code == 0) {
                    success(data.message);
                    setTimeout("location.reload()",1000);
                }else{
                    error(data.message);
                }
            }
        })
    });

    //编辑分类表单提交
    $("#edit-button").click(function(){
        var targetUrl = $("#edit-form").attr("action");
        var data = $("#edit-form").serialize();
        $.ajax({
            type:'post',
            url:targetUrl,
            cache: false,
            data:data,
            dataType:'json',
            success:function(data){
                if(data.code == 0) {
                    success(data.message);
                    setTimeout("location.reload()",1000);
                }else{
                    error(data.message);
                }
            }
        })
    });

    //更改状态
    function change_status(id,type,field) {
        $.ajax({
            url:'/admin/system/changeStatus',
            method: 'POST',
            dataType: 'json',
            data:{
                id:id,
                type:type,
                field:field
            },
            success: function(res)
            {
                if(res.code != 0) {
                    error(res.message);
                }
            }
        })
    };

    //删除视频
    function deleted(id,type) {
        if(confirm("是否确认删除")){
            $.ajax({
                url:'/admin/system/deleted',
                method: 'POST',
                dataType: 'json',
                data:{
                    id:id,
                    type:type
                },
                success: function(res)
                {
                    if(res.code == 0) {
                        success(res.message);
                        setTimeout("location.reload()",1000);
                    }else{
                        error(res.mssage);
                    }
                }
            })
        }
    };

</script>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        var i = 1,
        $example_dropzone_filetable = $("#example-dropzone-filetable"),
        example_dropzone1 = $("#advancedDropzone").dropzone({
            url: '/admin/video/uploadFile',
            // Events
            addedfile: function(file)
            {
                $('#upload-show').modal('toggle')
                if(i == 1)
                {
                    $example_dropzone_filetable.find('tbody').html('');
                }

                var size = parseInt(file.size/1024, 10);
                size = size < 1024 ? (size + " KB") : (parseInt(size/1024, 10) + " MB");

                var	$el = $('<tr>\
                                                <td class="text-center">'+(i++)+'</td>\
                                                <td>'+file.name+'</td>\
                                                <td><div class="progress progress-striped"><div class="progress-bar progress-bar-warning"></div></div></td>\
                                                <td>'+size+'</td>\
                                                <td>Uploading...</td>\
                                            </tr>');

                $example_dropzone_filetable.find('tbody').append($el);
                file.fileEntryTd = $el;
                file.progressBar = $el.find('.progress-bar');
            },

            uploadprogress: function(file, progress, bytesSent)
            {
                file.progressBar.width(progress + '%');
            },

            success: function(file,res)
            {
                file.fileEntryTd.find('td:last').html('<span class="text-success">Uploaded</span>');
                file.progressBar.removeClass('progress-bar-warning').addClass('progress-bar-success');
                $('#advancedDropzone').html('');
                $('#advancedDropzone').append('<img style="width: 180px;height: 120px;border-radius: 0%" src="' + res.data.url + '" />');
                $('#add-form #file_url').val(res.data.url);
                $('#upload-show').modal('toggle')
            },

            error: function(file)
            {
                file.fileEntryTd.find('td:last').html('<span class="text-danger">Failed</span>');
                file.progressBar.removeClass('progress-bar-warning').addClass('progress-bar-red');
            }
        });




        example_dropzone2 = $("#edit-advancedDropzone").dropzone({
            url: '/admin/video/uploadFile',
            // Events
            addedfile: function(file)
            {
                $('#upload-show').modal('toggle')
                if(i == 1)
                {
                    $example_dropzone_filetable.find('tbody').html('');
                }

                var size = parseInt(file.size/1024, 10);
                size = size < 1024 ? (size + " KB") : (parseInt(size/1024, 10) + " MB");

                var	$el = $('<tr>\
                                        <td class="text-center">'+(i++)+'</td>\
                                        <td>'+file.name+'</td>\
                                        <td><div class="progress progress-striped"><div class="progress-bar progress-bar-warning"></div></div></td>\
                                        <td>'+size+'</td>\
                                        <td>Uploading...</td>\
                                    </tr>');

                $example_dropzone_filetable.find('tbody').append($el);
                file.fileEntryTd = $el;
                file.progressBar = $el.find('.progress-bar');
            },

            uploadprogress: function(file, progress, bytesSent)
            {
                file.progressBar.width(progress + '%');
            },

            success: function(file,res)
            {
                file.fileEntryTd.find('td:last').html('<span class="text-success">Uploaded</span>');
                file.progressBar.removeClass('progress-bar-warning').addClass('progress-bar-success');
                $('#edit-advancedDropzone').html('');
                $('#edit-advancedDropzone').append('<img style="width: 180px;height: 120px;border-radius: 0%" src="' + res.data.url + '" />');
                $('#edit-form #edit-file_url').val(res.data.url);
                $('#upload-show').modal('toggle')
            },

            error: function(file)
            {
                file.fileEntryTd.find('td:last').html('<span class="text-danger">Failed</span>');
                file.progressBar.removeClass('progress-bar-warning').addClass('progress-bar-red');
            }
        });


    });
</script>
{/block}